package cetcbigdata.da.service.guangdong.at_once_onlinei_tem;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.base.DownloadAndUploadMinio;
import cetcbigdata.da.service.guangdong.util.GdUtil;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GuangDongPerson extends Base {
    private static String baseRegion = "广东省";

    private static String baseModule = "服务事项-“马上办、网上办、就近办、一次办”事项目录--个人";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                for(JSONObject m : getModule()){
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem){
        String regCode = regionItem.getString("code");
        String code =  moduleItem.getString("code");

        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");
        try{
            // 初始化总页码数
            int pages = 0;
            // 当前页
            int currentPage = 1;
            // 如果当前也不等于总页码数，则一直循环 ，实现翻页
            while (true){
                String listUrl = "https://www.gdzwfw.gov.cn/portal/api/v2/solr/servicetype/search";
                JSONObject headers = new JSONObject();
                headers.put("Referer", "https://www.gdzwfw.gov.cn/portal/v2/public/four-deal?region=" + regCode);
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
                JSONObject parmas = new JSONObject();
                parmas.put("level", 0);
                parmas.put("pageNum", 1);
                parmas.put("pageSize", 10);
                parmas.put("regionCode", regCode);
                JSONObject page = new JSONObject();
                JSONObject guide = new JSONObject();
                guide.put("page", currentPage);
                guide.put("pageSize", 10);
                page.put("guide", guide);
                parmas.put("page", page);
                List<String> searchType = new ArrayList<>();
                searchType.add("guide");
                parmas.put("searchType", searchType);
                List<String> serviceType = new ArrayList<>();
                searchType.add(code);
                parmas.put("serviceType", serviceType);
                parmas.put("taskTag", 1);
                String listResponse =  postReq(listUrl,parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);

                JSONArray listTemp = responseObj.getJSONObject("guide").getJSONArray("data");
                JSONObject listPageObj = new JSONObject();
                for(Object o : listTemp){
                    JSONObject item = (JSONObject) o;
                    String parentCatalogId = item.getString("groupKey");

                    if(listPageObj.containsKey(parentCatalogId)){
                        listPageObj.getJSONArray(parentCatalogId).add(item);
                    }else {
                        listPageObj.put(parentCatalogId, new JSONArray());
                        listPageObj.getJSONArray(parentCatalogId).add(item);
                    }
                }

                // 每页每项遍历
                for(String key : listPageObj.keySet() ){
                    JSONObject contentParent =new JSONObject();
                    String title = null;
                    String detailUrl = null;
                    String contentResponse = null;
                    for(Object o : listPageObj.getJSONArray(key)){
                        JSONObject item = (JSONObject) o;
                        detailUrl = String.format("https://www.gdzwfw.gov.cn/portal/v2/guide/%s",
                                item.getString("code"));
                        title = item.getString("highlightTitle");
                    }
                    contentParent.put("名称", title);
                    contentParent.put("网址", detailUrl);
                    processResult(listUrl, listResponse, detailUrl, contentResponse, contentParent,
                            region,
                            module
                    );

                }
                // 更新当前页
                currentPage = responseObj.getJSONObject("guide").getInteger("page") + 1;
                // 更新总页数
                pages = responseObj.getJSONObject("guide").getInteger("pageCount");
                if (currentPage > pages){
                    break;
                }
            }
            return new JSONObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject();
    }

    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{\"name\":\"广州市\",\"region\":\"440100\"},    \n" +
                "{\"name\":\"深圳市\",\"region\":\"440300\"}, \n" +
                "{\"name\":\"珠海市\",\"region\":\"440400\"},\n" +
                "{\"name\":\"汕头市\",\"region\":\"440500\"},\n" +
                "{\"name\":\"佛山市\",\"region\":\"440600\"},\n" +
                "{\"name\":\"韶关市\",\"region\":\"440200\"},\n" +
                "{\"name\":\"河源市\",\"region\":\"441600\"},\n" +
                "{\"name\":\"梅州市\",\"region\":\"441400\"},\n" +
                "{\"name\":\"惠州市\",\"region\":\"441300\"},\n" +
                "{\"name\":\"汕尾市\",\"region\":\"441500\"},\n" +
                "{\"name\":\"东莞市\",\"region\":\"441900\"},\n" +
                "{\"name\":\"中山市\",\"region\":\"442000\"},\n" +
                "{\"name\":\"江门市\",\"region\":\"440700\"},\n" +
                "{\"name\":\"阳江市\",\"region\":\"441700\"},\n" +
                "{\"name\":\"湛江市\",\"region\":\"440800\"},\n" +
                "{\"name\":\"茂名市\",\"region\":\"440900\"},\n" +
                "{\"name\":\"肇庆市\",\"region\":\"441200\"},\n" +
                "{\"name\":\"揭阳市\",\"region\":\"445200\"},\n" +
                "{\"name\":\"清远市\",\"region\":\"441800\"},\n" +
                "{\"name\":\"潮州市\",\"region\":\"445100\"},\n" +
                "{\"name\":\"云浮市\",\"region\":\"445300\"},\n" +
//                "{\"name\":\"横琴粤澳深度合作区\",\"region\":\"440405\"}\n" +
                " ]";
        return JSONArray.parseArray(city);
    }

    private static List<JSONObject> getRegion() {
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("region");

            //请求获取各市包含的区的名称和编码
            String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsNav/getDetail";
            JSONObject header = new JSONObject();
            header.put("Referer", "https://apps.gdzwfw.gov.cn/");
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0");
            JSONObject params = new JSONObject();
            params.put("regCode", code);

            //获取各个市的所有区的源数据
            String response = postFormReq(url, header, params);
            JSONObject responseObj = JSONObject.parseObject(response);
            if (responseObj.getString("statusCode").equals("200")){
                JSONArray dataList = responseObj.getJSONObject("data").getJSONObject("data").getJSONArray("country");

                //判断各个市是否包含区
                if (dataList.size() != 0){
                    for (Object d : dataList){
                        JSONObject dd = (JSONObject) d;
                        JSONObject outItem = new JSONObject();
                        String name = oo.getString("name") + "-" + dd.getString("ORGSNAME");
                        outItem.put("name", name);
                        outItem.put("code", dd.getString("ORGAREACODE"));
                        out.add(outItem);
                    }
                }else {
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name");
                    outItem.put("name", name);
                    outItem.put("code", oo.getString("region"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        //获取类型名称和编码
        String data = "{\"THEMES\":[{\n" + "label: \"马上办\",\n" + "value: \"1\"\n" + "}, {\n" + "label: \"网上办\",\n" + "value: \"2\"\n" + "}, {\n" + "label: \"就近办\",\n" + "value: \"3\"\n" + "}, {\n" + "label: \"一次办\",\n" + "value: \"4\"\n" + "}]}";
        JSONArray validData = JSONObject.parseObject(data).getJSONArray("THEMES");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : validData){
            JSONObject oItem = (JSONObject) o;
            JSONObject outItem = new JSONObject();
            outItem.put("code", oItem.getString("value"));
            outItem.put("name", oItem.getString("label"));
            out.add(outItem);
        }
        return out;
    }

    public static void main(String[] args) {
        GuangDongPerson spider = new GuangDongPerson();
        spider.getData(null, new ArrayList<>());
    }
}
